package com.alpha.bom;

import java.util.ArrayList;

/**
 * This class is used to represent the administrator of the test engine system.
 * 
 * @author John Gabriel Canoy
 * @version 1.0
 * @since JDK1.7
 */
public class Professor extends User {
	private ArrayList<Student> students;
	private ArrayList<Exam> exams;
	
	/**
	 * This is the default constructor for the Professor class. Initially, 
	 * sets the username to "root" and password to "password" which can be changed later. 
	 */
	public Professor() {
		this.setUserId(1000);
		this.setPassword("password");
		this.students = new ArrayList<Student>();
		this.exams = new ArrayList<Exam>();
		this.setUserType("Professor");
	}

	/**
	 * This methods retrieves the list of students of the professor.
	 * @return list of students of the professor
	 */
	public ArrayList<Student> getStudents() {
		return students;
	}

	/**
	 * This method sets the list of students of the professor with the one indicated.
	 * @param Students list of new students
	 */
	public void setStudents(ArrayList<Student> students) {
		this.students = students;
	}

	/**
	 * This method retrieves the list of exams made by the professor.
	 * @return the list of exams made by the professor 
	 */
	public ArrayList<Exam> getExams() {
		return exams;
	}
	
	/**
	 * This method sets the list of exams with the one indicated.
	 * @param exams list of exams made by the professor
	 */
	public void setExams(ArrayList<Exam> exams) {
		this.exams = exams;
	}
	
	
	
	/**
	 * This method adds a new student to the professor's list of students. The new student's ID number must not be in the list before the addition. 
	 * @param student Student object
	 * @return the student entry if the student is added to the list successfully, returns null otherwise.
	 */
	public Student addStudent(Student student) {
		if(!students.contains(student)) {
			students.add(student);
			student = students.get(students.size()-1);
		} else {
			student = null;
		}
		return student;
	}
	
	/**
	 * This method adds a new student to the professor's list of students. The new student's ID number must not be in the list before the addition.
	 * @param studentId int of the student ID
	 * @return returns the student entry if the student is added to the list successfully, returns null otherwise.
	 */
	public Student addStudent(int studentId) {
		return addStudent(new Student(studentId));
	}
	
	/**
	 * This method deactivates the status of the student. 
	 * @param student student object
	 * @return true if successfully deactivated the student, otherwise, false.
	 */
	public boolean deactivateStudent(Student student) {
		boolean deactivated = false;
		int index = students.indexOf(student);
		if(index != -1) {
			students.get(index).setActivated(false);
			deactivated = true;
		}
		return deactivated;
	}
	
	/**
	 * This method deactivates the status of the student. 
	 * @param studentId ID of the student
	 * @return true if successfully deactivated the student, otherwise, false.
	 */
	public boolean deactivateStudent(int studentId) {
		return deactivateStudent(new Student(studentId));
	}
	
	/**
	 * This method adds an exam to the list of exams.
	 * @param exam exam made by the professor
	 * @return the exam entry if the exam is successfully added to the list, otherwise, null
	 */
	public Exam addExam(Exam exam) {
		if(!exams.contains(exam)) {
			exams.add(exam);
			exam = exams.get(exams.size()-1);
		} else {
			exam = null;
		}
		return exam;
	}
	
	/**
	 * This method locks the exam indicated.
	 * @param exam exam to be locked
	 * @return true if the exam is successfully locked, otherwise, false.
	 */
	public boolean lockExam(Exam exam) {
		boolean locked = false;
		int index = exams.indexOf(exam);
		if(index != -1) {
			((RealExam) exams.get(index)).setLocked(true);
			locked = true;
		}
		return locked;
	}
	
	/**
	 * This method locks the exam with the indicated id.
	 * @param examId id of exam to be locked
	 * @return true if the exam is successfully locked, otherwise, false.
	 */
	public boolean lockExam(int examId) {
		return lockExam(new RealExam(examId));
	}
	
	/**
	 * This method unlocks the exam indicated.
	 * @param exam exam to be locked
	 * @return true if the exam is successfully locked, otherwise, false.
	 */
	public boolean unlockExam(Exam exam) {
		boolean unlocked = false;
		int index = exams.indexOf(exam);
		if(index != -1) {
			((RealExam) exams.get(index)).setLocked(false);
			unlocked = true;
		}
		return unlocked;
	}
	
	/**
	 * This method unlocks the exam with the indicated id.
	 * @param examId exam to be locked
	 * @return true if the exam is successfully locked, otherwise, false.
	 */
	public boolean unlockExam(int examId) {
		return unlockExam(new RealExam(examId));
	}
	
	/**
	 * This allows the professor to search for the student with the given student/user ID.
	 * @param studentId Student ID number
	 * @return The student with the given student ID.
	 */
	public Student searchStudent(int studentId){
		Student student = null;
		for(Student s: this.students){
			if (s!=null){
				if(s.getUserId()==studentId){
					student = s;
				}
			}
		}
		return student;
	}
}
